<?php
// $Id$

/**
 * Controller_Admin_Login 控制器
 */
class Controller_Admin_Login extends Controller_Abstract
{
	function actiondropuser()
	{
		Acctuser::meta()->deleteWhere('id = '.$_SESSION['acct']['id']);
		return $this->_redirectMessage('注销成功', '您已经成功注销!', url('login/index'));exit;
	}
	
	function actionIndex()
	{
		//
		$admin_login_form = new Form_Adminlogin(url(''));
		if ($this->_context->isPOST() )
		{
			$username = $this->_context->post('username');
			$userpass = $this->_context->post('userpass');
			$rnd  = $this->_context->post('rndcode');
			if (!Helper_Ckcode::check($rnd)){
				return $this->_redirectMessage('登陆失败', '验证码错误', url('login/index'));
			}else{
				$flag = Acct::check_user_pass($username,$userpass);
				if ($flag == false)
				{
					return $this->_redirectMessage('登陆失败', '账号密码错误', url('login/index'));
				}else{
					$filename = Q::ini('app_config/ROOT_DIR').'/config/power.yaml';
						$set = Helper_YAML::load($filename);
						$power = $set['power'];
					if (isset($power[$_SESSION['acct']['acclevel']]))
					{
						$user_power = $power[$_SESSION['acct']['acclevel']];
						foreach ($user_power as $k=>$v)
						{
							$user_power[$k] = explode(',', $v);
						}
						$_SESSION['power'] = $user_power;
						Syslog::logs('登陆成功');
					}else
					{
						return $this->_redirectMessage('登陆失败', '对不起,您当前的用户组没有任何权限', url('login/loginout'));
						exit();
					}
					return $this->_redirectMessage('登陆成功', '帐号登录成功', url('login/main'));
					//header('location:'.url('login/main'));
				}
			}
			
		}
		//$this->_view['form'] = $admin_login_form;
	}
	/**
	 *
	 * 账户退出
	 */
	function actionLoginout()
	{
		if(isset($_SESSION['acct']))
		{Syslog::logs('退出登陆');
		unset($_SESSION['acct']);}
		header('location:'.url('login'));
	}
	function actionmain()
	{
		$this->_check_Admin_Login();

		$top_main = helper_menu_level::_get_UserMenu($_SESSION['acct']['acclevel']);

		$this->_view['top_menus'] = $top_main;
	}
	function actionstartmain()
	{
		
		$years = date( "Y" );
		$month = date( "m" );
		if ( strlen( $month ) == 1 )
		{
				$month = "0".$month;
		}
		$tablename = 'cost'.$years.$month ;
		if ( trim( $tablename ) != "" )
		{
				$timestamp = time( );
				$timestamp -= 86400;
				$datetime = date( "Y-m-d H:i:s", $timestamp );
				if ($_SESSION['acct']['admin_id'] == -1 && $_SESSION['acct']['agent_id'] == -1)
				{
				//为管理
					//$append['']
					//$append['admin_id'] = $_SESSION['acct']['id'];
					$result = ' and 1=1';
				}else
				{
// 					$agentids = Acct::_get_have_acctid($_SESSION['acct']['id'], $_SESSION['acct']['admin_id']);
// 					if (count($agentids))
// 					{
// 						$agentids = array_values($agentids);
						
// 						$agentids[]= $_SESSION['acct']['id'];
						
// 					}else{
// 						$agentids = array($_SESSION['acct']['id']);
// 					}
// 					$array = array();
					
// 					foreach ($agentids as $_v=>$_k)
// 					{
// 						$array[] = ' agent_id = '.$_k.' ';
// 					}
					//$result = ' and ('. implode(' OR ', $array) .')';
					//$result = ' and agent_id in ('.implode(',', $agentids).')';
					$result = ' and agent_id = '.intval($_SESSION['acct']['id']);
					
				}
				$sql = "select  sum(billsec) as billsec ,sum(cost) as cost,sum(fees) as fees  FROM  ".$tablename." where start_stamp >='".$datetime."' ".$result;				
				if ($_SESSION['acct']['admin_id'] != -1 && $_SESSION['acct']['agent_id'] != -1)
				{
					try {
						$result = QDB::getConn()->getAll($sql);
					} catch (Exception $e) {
						$result = false;
					}
				}else{
					$result = false;
				}
					
				$res = 0;
				$sum_billsec = 0;
				$sum_cost = 0;
				$sum_fees = 0;
				if ( $result )
				{
						$res = $result[0];
						
						$sum_billsec = $res['billsec'];
						$sum_cost = $res['cost'];
						$sum_fees = $res['fees'];
						
				}else{
					    $res = array(
					    		'billsec' =>0,
					    		'cost' =>0,
					    		'fees' =>0
					    		);
				}
				if ( trim( $sum_billsec ) == "" )
				{
						$sum_billsec = 0;
				}
				
				$sum_profit = $sum_fees - $sum_cost;
				$this->_view['sum_profit'] = $sum_profit;
				$this->_view['baseinfo'] = $res;
		}
	}
	function actionChangePwd()
	{
		if (!isset($_SESSION['acct'])){
			exit('未登陆');
		}else {
			if ($this->_context->isGET())
			{	
					
			}else {
				//post
			$New_Pwd = $this->_context->Newpassword;
					$New_Pwd_cfg = $this->_context->Newpassword_config;
					$old_pwd = $this->_context->o_password;
					if (strlen($old_pwd) == 0 || strlen($New_Pwd) == 0 )
					{
						return $this->error('数据不完整');
					}else if($New_Pwd != $New_Pwd_cfg)
					{
						return $this->error('两次新密码不一致');
					}else 
					{
						$New_Pwd = ($New_Pwd);
						$old_pwd = ($old_pwd);
						if($_SESSION['acct']['acclevel'] == 3)
						{
						$user = Acctuser::find('acctname = ? and password = ?',$_SESSION['acct']['acctname'],$old_pwd)->getOne();
						}else {
						$user = Acct::find('acctname = ? and password = ?',$_SESSION['acct']['acctname'],$old_pwd)->getOne();
						}
						if($user->id())
						{
							$user->password = $New_Pwd;
							$user->save(0,'update');
							QDB::getConn()->execute("update sip_bindexten_buddies set secret ='{$New_Pwd}' where accountcode = '{$_SESSION['acct']['acctname']}'");
							return $this->success('修改成功,请重新登陆');
							Syslog::logs('修改密码');
						}else {
							return $this->error('原始密码错误');
						}
					
					}
			}
			
		}
	}
	function actionbindtel()
	{
		if (!isset($_SESSION['acct'])){
			exit('未登陆');
		}else {
			if($_SESSION['acct']['acclevel'] <> 3)
			{
				exit('not perssion');
			}
			if ($this->_context->isGET())
			{
					$bindtel = Acctuser::find('id = ?',$_SESSION['acct']['id'])->setColumns('id,bindtel')->getOne();
					$this->_view['bind'] = $bindtel;
			}else {
				$newBindTel = trim($this->_context->bindtel);
				if ($newBindTel)
				{
					$ishave = Acctuser::find('bindtel = ? and id <> ?',$newBindTel,$_SESSION['acct']['id'])->getCount('id');
					if($ishave)
					{
						return $this->error('对不起,已经存在此绑定号码');
					}
				}
				
				$acctuser = Acctuser::find('id = ?',$_SESSION['acct']['id'])->getOne();
				$acctuser->bindtel = $newBindTel;
				$acctuser->save(0,'update');
				return $this->success('修改额外绑定号码成功');
			}
		}
	}
}